package com.hsz.email.util;

import com.sun.mail.util.MailSSLSocketFactory;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.util.Properties;

public class MailUtil {

    public static void mail(String email, String username, String title, String verification) throws Exception {
        Properties prop = new Properties();
        prop.setProperty("mail.host", "smtp.qq.com"); // 设置QQ邮件服务器
        prop.setProperty("mail.transport.protocol", "smtp"); // 邮件发送协议
        prop.setProperty("mail.smtp.auth", "true"); // 需要验证用户名和密码

        // 对于QQ邮箱，还需要设置SSL加密，加上以下代码即可
        MailSSLSocketFactory sf = new MailSSLSocketFactory();
        sf.setTrustAllHosts(true);
        prop.put("mail.smtp.ssl.enable", "true");
        prop.put("mail.smtp.ssl.socketFactory", sf);

        // 使用JavaMail发送邮件的5个步骤
        // 1、创建定义整个应用程序所需的环境信息的Session对象
        Session session = Session.getDefaultInstance(prop, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                // 发件人邮箱用户名、授权码
                return new PasswordAuthentication("1727635552@qq.com", "qdmtdljhwwqtceed");
            }
        });

        // 开始Session的debug模式，这样可以看到程序发送Email的运行状态
        session.setDebug(true);

        // 2、通过Session得到transport对象
        Transport ts = session.getTransport();

        // 3、使用邮箱的用户名和授权码连上邮件服务器
        ts.connect("smtp.qq.com", "1727635552@qq.com", "qdmtdljhwwqtceed");

        // 4、创建邮件:写邮件
        // 注意需要传递Session
        MimeMessage message = mailWithImgAndFile(session,email,username,title,verification);

        // 5、发送邮件
        ts.sendMessage(message, message.getAllRecipients());

        // 6、关闭连接
        ts.close();
    }

    public static MimeMessage mailWithImgAndFile(Session session,String email,String username,String title ,String verification) throws Exception {
        // 消息的固定信息
        MimeMessage mimeMessage = new MimeMessage(session);
        //指明发件人
        mimeMessage.setFrom(new InternetAddress("1727635552@qq.com"));
        //指明收件人
        mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(email));
        //邮件的标题
        mimeMessage.setSubject(title);

        /*
        1、图片
        2、附件
        3、文本
         */
        //准备图片数据
//        MimeBodyPart image = new MimeBodyPart();
//        DataHandler dh = new DataHandler(new FileDataSource("D:\\springboot\\email\\src\\main\\resources\\static\\img\\bg.jpg"));
//        image.setDataHandler(dh); //在body中放入这个处理的图片数据
//        image.setContentID("bz.jpg");//给图片设置一个ID，在后面使用

        //准备文本数据
        MimeBodyPart text = new MimeBodyPart();
        text.setContent("<table style=\"margin: 0 auto\" width=\"60%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n" +
                "\n" +
                "        <tbody>\n" +
                "            <tr>\n" +
                "                <td class=\"p-80 mpy-35 mpx-15\" bgcolor=\"#212429\" style=\"padding: 80px;\">\n" +
                "                    <table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n" +
                "\n" +
                "\n" +
                "                        <tbody>\n" +
                "                            <tr>\n" +
                "                                <td class=\"img pb-45\"\n" +
                "                                    style=\"font-size:0pt; line-height:0pt; text-align:left; padding-bottom: 45px;\">\n" +
                "                                    <a href=\"https://store.steampowered.com/\" target=\"_blank\" rel=\"noopener\">\n" +
                "                                        <img src=\"https://store.cloudflare.steamstatic.com/public/shared/images/email/logo.png\"\n" +
                "                                            width=\"615\" height=\"88\" border=\"0\">\n" +
                "                                    </a>\n" +
                "\n" +
                "                                </td>\n" +
                "                            </tr>\n" +
                "\n" +
                "\n" +
                "\n" +
                "                            <tr>\n" +
                "                                <td>\n" +
                "\n" +
                "                                    <table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n" +
                "                                        <tbody>\n" +
                "                                            <tr>\n" +
                "                                                <td class=\"title-36 pb-30 c-grey6 fw-b\"\n" +
                "                                                    style=\"font-size:36px; line-height:42px; font-family:'Motiva Sans', Helvetica, Arial, sans-serif; text-align:left; padding-bottom: 30px; color:#bfbfbf; font-weight:bold;\">\n" +
                "                                                    "+username+"，您好！</td>\n" +
                "                                            </tr>\n" +
                "                                        </tbody>\n" +
                "                                    </table>\n" +
                "                                    <table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n" +
                "                                        <tbody>\n" +
                "                                            <tr>\n" +
                "                                                <td class=\"text-18 c-grey4 pb-30\"\n" +
                "                                                    style=\"font-size:18px; line-height:25px; font-family:'Motiva Sans', Helvetica, Arial, sans-serif; text-align:left; color:#dbdbdb; padding-bottom: 30px;\">\n" +
                "                                                    您注册帐户 "+username+" 所需的验证码 为：</td>\n" +
                "                                            </tr>\n" +
                "                                        </tbody>\n" +
                "                                    </table>\n" +
                "                                    <table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n" +
                "                                        <tbody>\n" +
                "                                            <tr>\n" +
                "                                                <td class=\"pb-70 mpb-50\" style=\"padding-bottom: 70px;\">\n" +
                "                                                    <table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"\n" +
                "                                                        bgcolor=\"#17191c\">\n" +
                "                                                        <tbody>\n" +
                "                                                            <tr>\n" +
                "                                                                <td class=\"py-30 px-56\"\n" +
                "                                                                    style=\"padding-top: 30px; padding-bottom: 30px; padding-left: 56px; padding-right: 56px;\">\n" +
                "                                                                    <table width=\"100%\" border=\"0\" cellspacing=\"0\"\n" +
                "                                                                        cellpadding=\"0\">\n" +
                "                                                                        <tbody>\n" +
                "                                                                            <tr>\n" +
                "                                                                                <td class=\"title-48 c-blue1 fw-b a-center\"\n" +
                "                                                                                    style=\"font-size:48px; line-height:52px; font-family:'Motiva Sans', Helvetica, Arial, sans-serif; color:#3a9aed; font-weight:bold; text-align:center;\">\n" +
                "                                                                                    "+verification+" </td>\n" +
                "                                                                            </tr>\n" +
                "                                                                        </tbody>\n" +
                "                                                                    </table>\n" +
                "                                                                </td>\n" +
                "                                                            </tr>\n" +
                "                                                        </tbody>\n" +
                "                                                    </table>\n" +
                "                                                </td>\n" +
                "                                            </tr>\n" +
                "                                        </tbody>\n" +
                "                                    </table>\n" +
                "                                    <table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n" +
                "                                        <tbody>\n" +
                "                                            <tr>\n" +
                "                                                <td class=\"text-18 c-grey4 pb-30\"\n" +
                "                                                    style=\"font-size:18px; line-height:25px; font-family:'Motiva Sans', Helvetica, Arial, sans-serif; text-align:left; color:#dbdbdb; padding-bottom: 30px;\">\n" +
                "                                                    您会收到这封自动产生的邮件，是由于您正在注册账户。<br><br>\n" +
                "                                                    Steam 令牌验证码是完成注册所必需的。\n" +
                "                                                </td>\n" +
                "                                            </tr>\n" +
                "                                        </tbody>\n" +
                "                                    </table>\n" +
                "                                    <table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n" +
                "                                        <tbody>\n" +
                "                                            <tr>\n" +
                "                                                <td class=\"text-18 c-blue1 pb-40\"\n" +
                "                                                    style=\"font-size:18px; line-height:25px; font-family:'Motiva Sans', Helvetica, Arial, sans-serif; text-align:left; color:#3a9aed; padding-bottom: 40px;\">\n" +
                "                                                </td>\n" +
                "                                            </tr>\n" +
                "                                        </tbody>\n" +
                "                                    </table>\n" +
                "\n" +
                "\n" +
                "                                    <table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n" +
                "                                        <tbody>\n" +
                "                                            <tr>\n" +
                "                                                <td class=\"pt-30\" style=\"padding-top: 30px;\">\n" +
                "                                                    <table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n" +
                "                                                        <tbody>\n" +
                "                                                            <tr>\n" +
                "                                                                <td class=\"img\" width=\"3\" bgcolor=\"#3a9aed\"\n" +
                "                                                                    style=\"font-size:0pt; line-height:0pt; text-align:left;\">\n" +
                "                                                                </td>\n" +
                "                                                                <td class=\"img\" width=\"37\"\n" +
                "                                                                    style=\"font-size:0pt; line-height:0pt; text-align:left;\">\n" +
                "                                                                </td>\n" +
                "                                                                <td>\n" +
                "                                                                    <table width=\"100%\" border=\"0\" cellspacing=\"0\"\n" +
                "                                                                        cellpadding=\"0\">\n" +
                "                                                                        <tbody>\n" +
                "                                                                            <tr>\n" +
                "                                                                                <td class=\"text-16 py-20 c-grey4 fallback-font\"\n" +
                "                                                                                    style=\"font-size:16px; line-height:22px; font-family:'Motiva Sans', Helvetica, Arial, sans-serif; text-align:left; padding-top: 20px; padding-bottom: 20px; color:#f1f1f1;\">\n" +
                "                                                                                    祝您愉快，<br>\n" +
                "                                                                                    Steam团队&电脑商城_HRBNU </td>\n" +
                "                                                                            </tr>\n" +
                "                                                                        </tbody>\n" +
                "                                                                    </table>\n" +
                "                                                                </td>\n" +
                "                                                            </tr>\n" +
                "                                                        </tbody>\n" +
                "                                                    </table>\n" +
                "                                                </td>\n" +
                "                                            </tr>\n" +
                "                                        </tbody>\n" +
                "                                    </table>\n" +
                "\n" +
                "\n" +
                "                                </td>\n" +
                "                            </tr>\n" +
                "\n" +
                "                        </tbody>\n" +
                "                    </table>\n" +
                "                </td>\n" +
                "            </tr>\n" +
                "\n" +
                "\n" +
                "\n" +
                "\n" +
                "\n" +
                "        </tbody>\n" +
                "    </table>","text/html;charset=utf-8");

//        //附件
//        MimeBodyPart file = new MimeBodyPart();
//        file.setDataHandler(new DataHandler(new FileDataSource("文件路径")));
//        file.setFileName("SimpleEmail.java");

        // 接下来先将图片和文本拼接，得到multipart1，然后再把multipart1和文件进行拼接
        MimeMultipart multipart1 = new MimeMultipart();
//        multipart1.addBodyPart(image);
        multipart1.addBodyPart(text);
        multipart1.setSubType("related");

        //将拼装好的正文内容设置为主题
        MimeBodyPart contentText = new MimeBodyPart();
        contentText.setContent(multipart1);

        //拼接文件
        MimeMultipart addFile = new MimeMultipart();
//        addFile.addBodyPart(file);
        addFile.addBodyPart(contentText); //正文（文本+图片）
        addFile.setSubType("mixed");

        //放到message消息中
        mimeMessage.setContent(addFile);
        mimeMessage.saveChanges();

        return mimeMessage;
    }
}
